package net.lovesrc.allin.biz.DogDog;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.MyHttpRequest;
import com.zhouzhou.fastjava.http.Requests;
import net.lovesrc.allin.biz.BaseService;
import net.lovesrc.allin.biz.gogohome.HomeAccountQueue;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class DogLoginSignService extends BaseService {

    public AtomicBoolean flag = new AtomicBoolean(true);
    public AtomicBoolean flagLogin = new AtomicBoolean(true);


    public void sign(JSONObject phone) {
        CookieStore cookieStore = new BasicCookieStore();
        HttpClient httpClient = newHttpClientBuilder(cookieStore).build();


        JSONObject jsonObject3 = MyHttpRequest.post(httpClient, "http://doge.oust.store/signin")
                .param("username", phone.getString("phone_number"))
                .param("password", phone.getString("password"))
                .execute().asJson();

        if (jsonObject3 == null || !"200".equals(jsonObject3.getString("code"))) {
            return;
        }


        MyHttpRequest.post(httpClient, "http://doge.oust.store/machine/profit")
                .execute().asJson();

    }


//    @Scheduled(fixedDelay = 60 * 60 * 1000)
//    @PostConstruct
    public void login() {
        //查数据库 定时签到
        List<Map<String, Object>> mgtUserInfos;
        try {
            int start = 0;
            int page = 100;
            while (!(mgtUserInfos = jdbcTemplate.queryForList("select * from t_gougou_user_info limit ?,?", start, start + page)).isEmpty() && flagLogin.get()) {
                mgtUserInfos.forEach(mgtUserInfo -> {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                        DogQueue.queue.put(JSON.parseObject(JSON.toJSONString(mgtUserInfo)));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
                start += page;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
